<template>
  <div class="notice">
    <HomeContent>
      <template #table-title>
        公告
        <div class="timeSelection" :style="{'width': noticeDate ? '200px' : '66px'}">
          <el-date-picker
              ref="datePicker"
              v-model="noticeDate"
              class="date-button"
              :style="{'opacity': noticeDate ? 1 : 0}"
              type="daterange"
              range-separator="-"
              value-format="yyyy-MM-dd"
              @change="changeDate">
            </el-date-picker>
            <el-button @click="displayDatePicker" :style="{'display': noticeDate ? 'none' : 'block'}" size="mini" class="mini-button"><span style="color:#767E91"><i class="V7_iconV7tubiao_rili V7_iconfont" style="font-size:12px;"></i> 时间</span></el-button>
        </div>
      </template>
      <template #table-content>
        <div class="notice-content" v-if="noticeList.length > 0">
          <el-scrollbar style="height:166px">
          <div class="notice-item" v-for="item in noticeList" :key="item._id" :class="{'notice-item-important': item.level === 'import'}">
                <div class="item-text" :title="item.title" @click="openDraw(item)"><i v-if="item.attachs.length > 0" class="V7_iconfont V7_iconV7tubiao_fujian" style="font-size:12px;color:#A6A6AE;padding-right:3px"></i>{{item.title}}</div>
                <div class="item-time">{{item.publishTime}}</div>
            </div>
            </el-scrollbar>
        </div>
        <div v-if="noticeList.length === 0" class="notice-empty">
              <img src="../../../assets/img/notice_empty.png" alt="" >
              <p>目前还没有公告哦～</p>
          </div>
      <div class="notice-footer" v-if="noticeList.length > 0">
        <el-pagination
            @current-change="handleCurrentChange"
            :current-page.sync="page"
            :pager-count="5"
            layout="prev, pager, next"
            :total="totalResult">
        </el-pagination>
      </div>
      </template>
    </HomeContent>
      <notice-drawer ref="NoticeDrawer"></notice-drawer>
  </div>
</template>

<script lang="ts">
import { Component, Vue, Watch } from 'vue-property-decorator'
import NoticeDrawer from './NoticeDrawer.vue'
import HomeContent from './HomeContent.vue'
import { commonAction } from '@/utils/common';
import { triggerEvent } from './home'
import { noticeConfigModule } from '@/store/modules/config/notice'
@Component({
  name: 'NoticeCard',
  components: {
    NoticeDrawer,
    HomeContent
  }
})
export default class NoticeCard extends Vue {
  [x: string]: any
  private noticeDate: any = null
  private noticeList: any = []
  private page: number = 1
  private totalResult: number = 0

  public activated () {
    if (this.page === 1) {
      this.getData()
    }
  }

  private mounted () {
    (this as any).$getCache('/base/action/general/cache/query', ['userList'], true).then((data: any) => {
      this.getData()
    })
  }

  get freshTime () {
    return noticeConfigModule.refreshNotice
  }

  @Watch('freshTime')
  private freshTimeChange () {
    this.page = 1
    this.getData()
  }

  private displayDatePicker () {
    triggerEvent(this.$refs.datePicker)
  }

  private async getData () {
    const actionData: any = {
      limit: 10,
      page: this.page
    }
    if (this.noticeDate) {
      actionData.publishTimeBegin = this.noticeDate[0] + ' 00:00:00'
      actionData.publishTimeEnd = this.noticeDate[1] + ' 23:59:59.999'
    }
    const params = { url: '/base/action/notice/workbench/noticeList', method: 'POST', data: actionData }
    const data: any = await commonAction(params)
    if (data.success) {
      const levelList = data.data.list.filter(v => v.level === 'import')
      const commonList = data.data.list.filter(v => v.level === 'common')
      this.noticeList = [...levelList, ...commonList]
      this.totalResult = data.data.count
    }
  }

  private handleCurrentChange (page: number) {
    this.page = page
    this.getData()
  }

  private changeDate () {
    this.page = 1
    this.getData()
  }

  private async openDraw (notice: any) {
    const params = { url: '/base/action/notice/workbench/getNoticeById', method: 'POST', data: { id: notice._id } }
    const result: any = await commonAction(params)
    if (result.success) {
      const { data } = result
      if (data) {
        (this.$refs.NoticeDrawer as any).openDraw(data)
      } else {
        this.$message.error('公告已失效或被删除，无法查看')
      }
    }
  }
}
</script>
<style lang="stylus" scoped>
.notice
    >>> .el-range__close-icon
        height 32px
    >>> .el-range-separator
        height 34px
    >>> .el-range__icon
        height 32px
    width 100%
    .timeSelection
      height: 24px
      float: right
      position: relative
      .date-button
        position: absolute
        width: 100%
        height: 100%
        top: 0
        left: 0
        cursor pointer
    .notice-content
        height 160px
        overflow hidden
        position relative
        border-top: 1px solid #ECF0F3
        border-bottom: 1px solid #ECF0F3
        .notice-item-important::before
          content: ''
          position: absolute
          top: 0
          left: 0
          border: 5px solid #F5642B
          border-bottom-color: transparent
          border-right-color: transparent
        .notice-item
            line-height 39px
            position: relative
            border-bottom: 1px solid #ECF0F3
            overflow: hidden
            padding: 0px 9px
            .item-text
                font-size 12px
                width calc(100% - 140px)
                font-family: PingFang SC
                font-weight: 400
                color: #303753
                overflow hidden
                text-overflow ellipsis
                white-space nowrap
                float left
                cursor pointer
                position: relative
            .item-text:hover
                color $c-main
            .item-time
                font-size 12px
                font-size: 12px;
                font-family: PingFang SC
                font-weight: 400
                color: #303753
                width 120px
                float right
    .notice-empty
        width 170px
        height: 200px
        margin 0 auto
        img
          height: 120px
        p
          text-align center
          color: #767e91
    .notice-footer
        text-align right
        margin-top: 8px

</style>
